package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.pojo.req.SendMessageReq;
import com.example.demo.service.impl.KafkaServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author CHAS
 * 2024/8/7 23:54
 **/
@Slf4j
@RefreshScope
@RestController
@RequestMapping("/kafka")
@Api(tags = "kafka相关接口")
public class KafkaController {
    private static final String TOPIC = "kafka-topic";
    @Resource
    private KafkaServiceImpl kafkaService;
    static int count = 0;
    @ApiOperation(value = "发送一条消息")
    @PostMapping("/sendMessage")
    public void sendMessage(@RequestBody @Validated SendMessageReq req) {
        User build = User.builder().username("谭声俊").password("1234567890"+count++).build();
        kafkaService.sendMessage(req.getTopic(), build);
        System.out.println("发送成功");
    }
    @ApiOperation(value = "暂停消费")
    @PostMapping("/stopConsume")
    public void stopConsume() {
        kafkaService.stopConsume();
    }
    @ApiOperation(value = "启动消费")
    @PostMapping("/startConsume")
    public void startConsume() {
        kafkaService.startConsume();
    }
}
